var app = angular.module('breadTrip', []);

// 解决jinja2和angular的花括号{{}}冲突的方法
app.config(['$interpolateProvider', function($interpolateProvider) {
    $interpolateProvider.startSymbol('{[');
    $interpolateProvider.endSymbol(']}');
}]);
app.controller('typeController', ['$scope', function($scope){
    $scope.data = obj.category;
    $scope.localObj = JSON.parse(localStorage.getItem('va_'+BT.context('productPK')+'_order'));
    $scope.position = localStorage.getItem('type_'+BT.context('productPK'));
    function getCurrentData(){
        var array = $scope.data;
        for(var i in array){
            if(array[i].selected == true){
                $scope.items = array[i];
            }
        }
    }
    // 获取 order_id,plan_id,device_id参数信息
    function getHrefSuffix(){
        var param = location.search.replace(/\?/, '').split('&'), 
            arr = [];
        param.filter(function(i){
            if(i.indexOf("plan_id") > -1 || i.indexOf("device_id") > -1 || i.indexOf("order_id") > -1){
                arr.push(i);
            }
        });
        return arr.join('&');
    }
    // 分类切换
    $scope.tapCtrl = function(name){
        var array = $scope.data;
        for(var i in array){
            if(array[i].name == name){
                array[i].selected = true;
            }else{
                array[i].selected = false;
            }
        }
        getCurrentData();
    };
    function setLocalStorage (value, name, price){
        if($scope.localObj){
            $scope.localObj.basic.filter(function(item, index){
                if(index == $scope.position){
                    var c = d = 1;
                    item.filter(function(i, j){
                        if(i.name_en == 'count'){
                            c = i.value;
                        }
                        if(i.name_en == 'days'){
                            d = i.value;
                        }
                        if(i.name_en == 'price'){
                            i.value = price;
                            i.price = price * c * d;
                        }
                        if(i.name_en == 'ptype'){
                            i.value = name;
                        }
                        if(i.name_en == 'pcategory'){
                            i.value = value;
                        }
                    });
                }  
            });
            localStorage['va_'+BT.context('productPK')+'_order'] = JSON.stringify($scope.localObj); 
        }  
    }
    // 选择类型
    $scope.selectType = function(value){
        var array = $scope.data,
            flag = false;
        if(array.length > 1) flag = true;
        array.filter(function(items, index){
            items.types.filter(function(i, j){
                if(i.value == value){
                    i.selected = true;
                    var name = i.name,
                        price = i.price, 
                        id = BT.context('productPK'),
                        url;
                    if(flag) name = name + '('+items.name+')';
                    setLocalStorage(i.value, name, price);
                    url = location.origin+'/product/' + BT.context('productPK')+'/order/?' + getHrefSuffix();
                    location.href = url;
                }else{
                    i.selected = false;
                }
            });
        });
    };
    // 初始化
    $scope.init = function(){
        $scope.$watch("localObj", function(){});
        getCurrentData();
    };
    $scope.init();
}]);
